/*
 * ShowContactPage.java
 * 
 * Version information
 * 
 * Mar 24, 2006
 * 
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.icontact.webapp.struts.action;

import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.amsoft.iservice.icontact.hibernate.ContactPage;
import net.amsoft.iservice.icontact.hibernate.Message;
import net.amsoft.iservice.icontact.service.IContact;
import net.amsoft.iservice.icontact.util.IContactConstants;
import static net.amsoft.iservice.icontact.util.IContactConstants.*;
import net.amsoft.iservice.util.IServiceUtil;
import net.amsoft.iservice.util.MetaData;
import net.amsoft.iservice.util.ResolverClient;
import net.amsoft.iservice.util.exception.IServiceException;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import static org.apache.struts.action.ActionMessages.*;

/**
 * The action class ShowContactPage displays the contact page of an i-name when
 * it is requested by any user to place a contact request.
 * 
 * @author rahul
 * 
 */
public class ShowContactPage extends BaseAction {
	public static final Logger oLogger = Logger
			.getLogger(ShowContactPage.class);

	public ActionForward process(ActionMapping oMapping, ActionForm oForm,
			HttpServletRequest oRequest, HttpServletResponse oResponse)
			throws Exception {
		oLogger.info("process() : entry");
		ActionMessages oMsgs = new ActionErrors();
		try {
			String sSynonym = oRequest.getParameter(REQ_PTR_SYN).toLowerCase();
			sSynonym = IServiceUtil.getIName(sSynonym);
                        
			String sHash = oRequest.getParameter(REQ_PTR_HASH);
			if (sHash != null && sHash.trim().length() > 0) {
				handleReference(oRequest, sHash.trim());
			}

			MetaData oMetaData = ResolverClient.getFinalMetaData(sSynonym);
			if (oMetaData == null) {
				oLogger.warn("process() : Metadata is null, IName: " + sSynonym
						+ " can't be resolved");
				oMsgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
						IContactConstants.ERR_INAME_UNRESOLVED));
				saveErrors(oRequest, oMsgs);
				return oMapping.findForward(FWD_FAILURE);
			}
			if (!IContact.isSynonymSubscribed(sSynonym, oMetaData
					.getGlobalInumber(), oRequest.getServerName())) {
				oLogger.warn("process() : Synonym: " + sSynonym
						+ " Not Subscribed");
				oMsgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
						IContactConstants.ERR_PAGE_NOT_EXIST));
				saveErrors(oRequest, oMsgs);
				return oMapping.findForward(FWD_FAILURE);
			}
			oRequest.getSession(false).setAttribute(SESSION_SYN, sSynonym);
			ContactPage oPage = IContact.getContactPage(sSynonym);
			oRequest.getSession(false).setAttribute(SESSION_CPAGE, oPage);

			return oMapping.findForward(FWD_SUCCESS);
		} catch (IServiceException e) {
			oLogger.error("process() : Exception: " + e.getMessage());
			if (e.getErrorCode() == 2100) {
				oMsgs.add(GLOBAL_MESSAGE,
						new ActionMessage(
								ResourceBundle.getBundle("icontactResources",
										oRequest.getLocale()).getString(
										IContactConstants.ERR_PAGE_NOT_EXIST),
								false));
			} else {
				oMsgs.add(GLOBAL_MESSAGE, new ActionMessage(e.getMessage(),
						false));
			}
			saveErrors(oRequest, oMsgs);
			return oMapping.findForward(FWD_FAILURE);
		}
	}

	/**
	 * This method is used to find the Message using the given hash value.
	 * 
	 * @param oRequest
	 *            Http Request
	 * @param sHash
	 *            Hash value of message
	 */
	private void handleReference(HttpServletRequest oRequest, String sHash) {
		oLogger.info("handleReference() : entry");
		try {
			oLogger.debug("handleReference(): HASH=" + sHash);
			Message oMsg = IContact.getMessageByHashAndState(sHash, ACTIVE);
			oMsg.update();
			if (oMsg != null) {
				oRequest.getSession(false).setAttribute(SESSION_REF, sHash);
			} else {
				oLogger.warn("handleReference(): HASH=" + sHash
						+ " Message not found");
				oRequest.getSession(false).setAttribute(SESSION_REF, null);
			}
		} catch (Exception e) {
			oLogger.warn("handleReference() : Exception : " + e.getMessage());
		}
	}
}
